#include <stdio.h>

int main (void)
{
    int count;
    int n,m;
    int loop,ctr,down,amount;

    scanf("%d",&count);
    while(count--)
    {
        down=1;
        scanf("%d %d",&n,&m);
        while(!(n==0 && m==0))
        {
            amount=0;
            for(loop=1;loop<n-1;loop++)
                for(ctr=loop+1;ctr<n;ctr++)
                    if ((loop*loop+ctr*ctr+m)%(loop*ctr) == 0)
                        amount++;
            printf("Case %d: %d\n",down++,amount);
            scanf("%d %d",&n,&m);
        }
        if (count)
            putchar('\n');
    }

    return 0;
}
